﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using W.Common.Enum;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Read.API.AppLication.Command.FinanceTypeCommand;
using W.Purchase.Read.API.AppLication.Command.ReceivableCommand;

namespace W.Purchase.Read.API.Controllers
{
    /// <summary>
    /// 应收管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReceivableController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<ReceivableController> _logger;
        public ReceivableController(IMediator mediator, ILogger<ReceivableController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 应收状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetReceivableStatus()
        {
            return Enum.GetValues(typeof(ReceivableStatus))
                .Cast<ReceivableStatus>()
                .Select(x => new
                {
                    Value = (int)x,
                    Text = x.ToString()
                }).ToList();
        }
        /// <summary>
        /// 应收管理列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Pages<ReceivableShowDto>>> GetReceivableList([FromQuery] ReceivableShowCommand command)
        {
            ApiResult<Pages<ReceivableShowDto>> result = new ApiResult<Pages<ReceivableShowDto>>();
            try
            {
                _logger.LogInformation("调用应收管理列表");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应收管理列表出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应收信息反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Receivable>> GetReceivableModel([FromQuery] ReceivableModelCommand command)
        {
            ApiResult<Receivable> result = new ApiResult<Receivable>();
            try
            {
                _logger.LogInformation("调用应收管理反填");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应收管理反填出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
